package event

import (
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"strconv"
	"strings"
)

// ConsumerMessage 消费者收到的消息
type ConsumerMessage struct {
	MsgId          string
	Topic          string
	Body           []byte
	ReconsumeTimes int32
	CompressedBody []byte
	Flag           int32
	TransactionId  string
	Batch          bool
	Compress       bool
	Properties     map[string]string
}

// GetKeys 获取消息的key
func (c *ConsumerMessage) GetKeys() []string {
	if len(c.Properties) == 0 {
		return nil
	}
	val, isOk := c.Properties[primitive.PropertyKeys]
	if !isOk {
		return nil
	}
	return strings.Split(val, primitive.PropertyKeySeparator)
}

// GetTags 获取消息的tag
func (c *ConsumerMessage) GetTags() string {
	if len(c.Properties) == 0 {
		return ""
	}
	// nolint
	val, _ := c.Properties[primitive.PropertyTags]
	return val
}

// GetShardingKey 获取消息的分区key
func (c *ConsumerMessage) GetShardingKey() string {
	if len(c.Properties) == 0 {
		return ""
	}
	// nolint
	val, _ := c.Properties[primitive.PropertyShardingKey]
	return val
}

// GetDelayTimeLevel 获取消息的延迟级别
func (c *ConsumerMessage) GetDelayTimeLevel() int {
	if len(c.Properties) == 0 {
		return 0
	}
	// nolint
	val, _ := c.Properties[primitive.PropertyDelayTimeLevel]
	level, _ := strconv.Atoi(val)
	return level
}
